package cn.cvs.servlet;

import cn.cvs.dao.MovieMapper;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/MovieListServlet")
public class MovieListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        int count=mapper.count();   //总记录数
        int pageNo=0;   //页码
        if (req.getParameter("pageNo")!=null){
            pageNo= Integer.parseInt(req.getParameter("pageNo"));
        }
        int pageCount=count%2==0?count/2:count/2+1; //总页数
        req.setAttribute("pageNo",pageNo);
        req.setAttribute("count",count);
        req.setAttribute("pageCount",pageCount);
        req.setAttribute("list",mapper.movieList(pageNo*2));
        req.getRequestDispatcher("/MovieList.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
